package cn.neusoft.luofa603.yuejian_29.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.List;
import cn.neusoft.luofa603.yuejian_29.Global;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.activity.ActivityInfo;
import cn.neusoft.luofa603.yuejian_29.adapter.ActivityAdapter;
import cn.neusoft.luofa603.yuejian_29.adapter.UnionAdapter;
import cn.neusoft.luofa603.yuejian_29.bean.Union;
import cn.neusoft.luofa603.yuejian_29.model.UnionModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ActivityFragment extends Fragment {
    private String id;
    private View view;
    private String hits;
    private String thumb;
    private String rec_name;
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_activity, container, false);

        initview();
        initEvents();
        initData();
        return view;
    }

     public void initview(){

         recyclerView = (RecyclerView) view.findViewById(R.id.RV_union);
         recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
         recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
    public void initEvents(){
        UnionModel unionModel = new UnionModel();
        Call<List<Union>> call = unionModel.getUnion();
        call.enqueue(new Callback<List<Union>>() {
            @Override
            public void onResponse(Call<List<Union>> call, Response<List<Union>> response) {
                final List<Union> mDataList = response.body();
                ActivityAdapter activityAdapter = new ActivityAdapter(getActivity(),mDataList);
                recyclerView.setAdapter(activityAdapter);
                activityAdapter.setItemClickListener(new ActivityAdapter.MyItemClickListener(){
                    public void onItemClick(View view,int position){
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), ActivityInfo.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("Union",mDataList.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });


            }

            @Override
            public void onFailure(Call<List<Union>> call, Throwable t) {
                Toast.makeText(getActivity(), "与服务器连接失败！", Toast.LENGTH_SHORT).show();

            }
        });

    }
    public void initData(){

    }

}
